/**
 *
 */

var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url: app.setting.att_url,
		pagedata:[],
		action: '',
		pagetitle:'支付页面',
	},
	onReady: function (){
		var the = this
	},
	onShow: function (){
		var ssid=wx.getStorageSync('ssid');
		if(!ssid && !app.issession){
			app.util.session();
		}
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (options){
		var the =this
		wx.setNavigationBarTitle({'title':the.data.pagetitle});
		the.getpagedata();
	},
	getpagedata: function (){
		var the=this;
		app.util.get(app.access,function(pagedata){
			//console.log('set');console.log(pagedata);
			pagedata.post.diyformfields=pagedata.post.diyfields;
			the.setData({
				pagedata:pagedata,
				pluginnav:app.session.menus,
				action:app.access.plugin+'/'+app.access.action+'/',
			});
		});
	},
	diyform_input:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var value = e.detail.value
		var data = e.currentTarget.dataset;
		//console.log(value)
		if(pagedata.post && pagedata.post.diyformfields && pagedata.post.diyformfields[data.childindex]){
			pagedata.post.diyformfields[data.childindex].data = value?value:data.value
			pagedata.post.diyformfields[data.childindex].selectkey = data.index
			//console.log(pagedata.post.diyformfields)
			the.setData({
				'pagedata.post.diyformfields':pagedata.post.diyformfields
			})
		}
    },
	diyform_duoxuan:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset;
		var value = e.detail.value
		console.log(value)
		if(pagedata.post && pagedata.post.diyformfields && pagedata.post.diyformfields[data.childindex]){
			pagedata.post.diyformfields[data.childindex].data = value
			the.setData({
				'pagedata.post.diyformfields':pagedata.post.diyformfields
			})
		}
    },
	bindRegionChange:function(e) {
		var the = this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset
		var region = e.detail.value
		pagedata.post.diyformfields[data.childindex].data.province = region[0]
		pagedata.post.diyformfields[data.childindex].data.city = region[1]
		pagedata.post.diyformfields[data.childindex].data.district = region[2]
		the.setData({
		  region: region,
		  'pagedata.post.diyformfields':pagedata.post.diyformfields
		})
    },
	up_file:function(e){
		var the =this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset
		app.util.up_file('image',function(res){
			console.log(res)
			if(data.source_type=='images'){
				pagedata.post.diyformfields[data.childindex].data.push(res);
			}else{
				pagedata.post.diyformfields[data.childindex].data = res
			}
			the.setData({
				'pagedata.post.diyformfields':pagedata.post.diyformfields
			})	
		});
	},
	closepaymodal:function(){
		var page=this
		page.setData({
			showpaymodal:false
		});
	},
	pay_credit:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_credit(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.success_url);
		},pagedata.paymodal.di_open)
	},
	pay_wechat:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_wechat(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.success_url);
		},pagedata.paymodal.di_open);
	},
	pay_alipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_alipay(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.success_url);
		},pagedata.paymodal.di_open);
	},
    pay_ttpay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_ttpay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.success_url);
		},pagedata.paymodal.di_open)
	},
	pay_hdfk:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_hdfk(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.success_url);
		})
	},
	pay_apipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_apipay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.success_url);
		},pagedata.paymodal.di_open,pagedata.paymodal.api_name)
	},
	radiotype:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		the.setData({
			'pagedata.paymodal.paytype':data.type,
			'pagedata.paymodal.api_name':data.name
		})
	},
	radiodi_open:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var di_open=pagedata.paymodal.di_open;
		di_open=di_open==data.type?'':data.type;
		if(!di_open){
			data.difee=0;
		}
		the.setData({
			'pagedata.paymodal.di_open':di_open,
			'pagedata.paymodal.di_fee':data.difee
		})
	},
	new_gotopay:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var paytype=pagedata.paymodal.paytype;
		if(paytype=='credit'){
			the.pay_credit();
		}else if(paytype=='wechat'){
			the.pay_wechat();
		}else if(paytype=='alipay'){
			the.pay_alipay();
		}else if(paytype=='hdfk'){
			the.pay_hdfk();
		}else if(paytype=='apipay'){
			the.pay_apipay();
		}else if(paytype=='ttpay'){
			the.pay_ttpay();
		}else if(paytype=='diypay'){
			app.util.pay_diypay(pagedata.paymodal);
		}else if(paytype=='diypay2'){
			app.util.pay_diypay2(pagedata.paymodal);
		}else{
			app.util.msg('请选择支付方式！');
		}
	},
	pay: function (e){
		var page=this
		var data = e.currentTarget.dataset;
		var pagedata=page.data.pagedata
		app.util.pay(pagedata.id,function(res){
				if(res.type=='more'){
					page.setData({
						showpaymodal:true,
						'pagedata.paymodal':res
					});
				}else if(res.type=='success'){
					app.util.msg(res.message,'',pagedata.success_url);
				}else{
					app.util.msg(res.message);
				}
		});
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		var the=this;
		app.util.getUserInfo(e,function(res){
			the.getpagedata();
		});
		the.setData({
			ShowAuthM:false
		})
	},
	mobile_login:function(e){
		var the=this
		var pagedata=the.data.pagedata
		//根据openid为用户登陆，此处尝试获取用户手机号
		app.util.post('core/member.set/setmobile/',e.detail,function(res){
			//console.log(res);
			pagedata.item.mobile=res.mobile
			the.setData({
				'pagedata.item':pagedata.item
			})
		});
	},
	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		//console.log(evt);
		app.util[data.func](data.params);
	},
	copy:function(e){
		var data=e.currentTarget.dataset;
		wx.setClipboardData({
		  data: data.msg,
		  success (res) {
			wx.getClipboardData({
			  success (res) {
				console.log(res.data) // data
			  }
			})
		  }
		})
	},
	formSubmit:function(){
		var page=this
		var pagedata=page.data.pagedata
        var str = /^[1][3,4,5,7,8][0-9]{9}$/ //手机
		var strname =  /^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$/ //中文
		var stremail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
		pagedata.post.diyfields=pagedata.post.diyformfields;
		var filedata = pagedata.post.diyfields?pagedata.post.diyfields:[]
		for (var i=0;i<filedata.length;i++){
			if((filedata[i].ismust ==1 || filedata[i].ismust ==1) && !filedata[i].data){
				app.util.msg(filedata[i].title +'-不能为空！','error');
				return false
			}
			if(filedata[i].type ==8 && (filedata[i].ismust ==1 || filedata[i].ismust ==1)){
				if(!(str.test(filedata[i].data))){
				  app.util.msg("请输入正确的手机号码！",'error')
				   return false;
				}
			}
			if(filedata[i].type ==9 && (filedata[i].ismust ==1 || filedata[i].ismust ==1)){
				if(!(stremail.test(filedata[i].data))){
				  app.util.msg("请输入正确的邮箱！",'error')
				   return false;
				}
			}
		}
		app.util.showLoading('下单中...');
		app.util.post('/member.pay/diypay/',pagedata.post);
	}
})